Repository navigation
fix(mcp): shut down on host disconnect to prevent orphan process trees - #3
Merged
Merged
Conversation
When the MCP host (e.g. Claude Code) exited, the spawned code-executor kept
running along with its whole downstream pool (Hermes + nested code-executor),
accumulating orphan trees across sessions. On macOS a child gets no signal when
its parent dies, and the SDK's StdioServerTransport listens only for
'data'/'error' on stdin — it never turns the EOF from the closed pipe into a
transport close. With nothing acting on the EOF the process never reached
'exit', so even the process.on('exit') reap backstop never fired.
Watch process.stdin for 'end'/'close' after the transport connects and trigger
the existing graceful shutdown() (which disconnects the downstream pool,
SIGTERM/SIGKILLs its children, then exits). Logic lives in a standalone,
side-effect-free module so it is unit-testable without the server's startup
side effects.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the MCP host (e.g. Claude Code) exited, the
code-executorit spawned kept running — taking its whole downstream pool (Hermes + nested code-executor) with it. Across multiple host sessions, entire orphan process trees accumulated (observed: 12 code-executor + 6 hermes from 4 dead sessions, while only 2 hosts were live).Root cause
On macOS a child process receives no signal when its parent dies (no
PR_SET_PDEATHSIGequivalent). The only reliable signal that the host is gone is EOF on stdin (the closed pipe). But the SDK'sStdioServerTransportlistens only for'data'/'error'on stdin — it never translates that EOF into a transport close. With nothing acting on the EOF, the process never reached'exit', so even the existingprocess.on('exit')reap backstop never fired.Note: a graceful host exit (host sends SIGTERM) always tore down cleanly via the existing signal handlers. The orphaning happened specifically when the host did not signal — it just closed the pipe.
Fix
Watch
process.stdinfor'end'/'close'after the transport connects, and trigger the existing gracefulshutdown()(which drains, disconnects the downstream pool, SIGTERM→SIGKILLs its children, thenprocess.exit(0)).The logic lives in a standalone, side-effect-free module (
src/mcp/stdin-watcher.ts) so it is unit-testable without the server's startup side effects (signal handlers,server.start()).Changes
src/mcp/stdin-watcher.ts(new) —watchClientDisconnect(stdin, onDisconnect), fires once on'end'/'close'.src/index.ts— wire it up right afterserver.connect(transport).tests/stdin-watcher.test.ts— 4 tests (end / close / both-fire-once / data-does-not-trigger).CHANGELOG.md— entry under the existing Unreleased "Fixed" block.Verification
npm run typecheck— clean.index.tsare pre-existing, out of scope).echo -n "" | timeout 10 ./bin/code-executor-mcp→stdin end — MCP client disconnected→ graceful shutdown → exit 0 (not a 124 hang).🤖 Generated with Claude Code